package com.servlet;
import java.io.*;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import com.beans.GoodsInfo;
import com.dao.GoodsDao;
import com.utils.PageInfo;
import com.utils.PageUtil;


@MultipartConfig  //因为有文件上传,所以不要忘了这个注解
@WebServlet("/GoodsServlet.do")
public class GoodsServlet extends HttpServlet  {
	private static final long serialVersionUID = 1L;
	private GoodsDao goodsDao=new GoodsDao();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		String flag=request.getParameter("flag");
		if("add".equals(flag)){
			this.addGoodsAndPic(request,response);
		}
		
		else if("manage".equals(flag)){
			this.manage(request,response);
		}
		
		else if("update".equals(flag)){
			this.update(request,response);
		}
		
		else if("del".equals(flag)){
			this.del(request,response);
		}
		else if("delMore".equals(flag)){
			this.delMore(request,response);
		}		
		
		else if("showPicture".equals(flag)){
			this.showPicture(request,response);
		}
	}
	
	//添加商品,并上传图片,用的是Servlet3.0提供的文件上传方式
	private void addGoodsAndPic(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {	
		//取得商品信息
	  	String goodsName = request.getParameter("goodsName");
	  	
	  	int bigCateId=Integer.parseInt(request.getParameter("bigCateId"));
	  	int smallCateId=Integer.parseInt(request.getParameter("smallCateId"));
	  	String unit=request.getParameter("unit");
		float price = Float.parseFloat( request.getParameter("price"));
		String producter=request.getParameter("producter");		
	    String des = request.getParameter("des");
	    
	    //把图片对应的输入流,转成字节数组
	    Part part = request.getPart("picture");//picture 是上传框(<input type="file" name="picture">) 的name属性
	    InputStream in=part.getInputStream();
	    byte [] pictureData=new byte[in.available()];
	    in.read(pictureData);
	   
	    GoodsInfo goods=new GoodsInfo();
		goods.setPictureData(pictureData);
	    goods.setGoodsName(goodsName);
	    goods.setBigCateId(bigCateId);
	    goods.setSmallCateId(smallCateId);
	    goods.setUnit(unit);
	    goods.setPrice(price);
	    goods.setProducter(producter);
	    goods.setDes(des);
	  
	    //添加商品,并得到添加的商品的id,主要用于在前台回显图片
	    int goodsId=goodsDao.addGoods(goods);
	    goods.setId(goodsId);

	    request.setAttribute("msg", "商品添加成功");
	    request.setAttribute("goods", goods);  //因为前端要进行数据回显,由于是图片上传表单,所以前端无法使用 ${param.属性名}的方式
	    request.getRequestDispatcher("/goods/goods_add.jsp").forward(request,response);      
	}
	
	//呈现出商品图片
	private void showPicture(HttpServletRequest request,	HttpServletResponse response) throws IOException {  		
		int goodsId= Integer.parseInt(request.getParameter("goodsId"));
  		GoodsDao dao=new GoodsDao();
  		GoodsInfo goods=  dao.getGoodsById(goodsId);
  		response.setContentType("image/jpg");
  		
  		ServletOutputStream outstream =response.getOutputStream();	
  		outstream.write(goods.getPictureData());
  		outstream.flush();
  	
  		//out.clear();
  		//out=pageContext.pushBody();	
	}

	//删除商品
	private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int goodsId= Integer.parseInt(request.getParameter("goodsId"));
		if(goodsDao.delGoodsById(goodsId)==1){
			request.setAttribute("msg", "商品信息删除成功!");
			this.manage(request, response);
		}	
	}
	//删除多个商品
	private void delMore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idSql=request.getParameter("idSql");
		idSql=idSql.substring(0,idSql.length()-1);
		String [] ids=idSql.split(",");
		for(int i=0;i<ids.length;i++) {
			Integer id=Integer.parseInt(ids[i]);
			goodsDao.delGoodsById(id);
		}
		request.setAttribute("msg", "商品信息删除成功!");
		this.manage(request, response);
		
	}


	//修改商品信息		
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    int goodsId=Integer.parseInt(request.getParameter("goodsId")); // 对于要修改的商品,是应该有ID的
	  	String goodsName = request.getParameter("goodsName");
	  	int bigCateId=Integer.parseInt(request.getParameter("bigCateId"));
	  	int smallCateId=Integer.parseInt(request.getParameter("smallCateId"));
	  	String unit=request.getParameter("unit");
		float price = Float.parseFloat( request.getParameter("price"));
		String producter=request.getParameter("producter");		
		String des = request.getParameter("des");
		
		GoodsInfo goods=goodsDao.getGoodsById(goodsId);
		 
	    goods.setGoodsName(goodsName);
	    goods.setBigCateId(bigCateId);
	    goods.setSmallCateId(smallCateId);
	    goods.setUnit(unit);
	    goods.setPrice(price);
	    goods.setProducter(producter);
	    goods.setDes(des);
	     
	    //处理图片数据
	    Part part= request.getPart("picture");
		if(  part.getSize()!=0){ 
			// 如果用户没有上传图片,	表示用户不想更改原来的图片,否则表示用户要改图片 
			InputStream in=part.getInputStream();
			byte [] pictureData=new byte [in.available()];
			in.read(pictureData);
			goods.setPictureData(pictureData);
		}

	    if(goodsDao.updateGoods(goods)==1)
	    {	
		    request.setAttribute("msg", "商品信息修改成功!");
		    request.setAttribute("goods", goods);  //因为前端要进行数据回显,由于是上传表单,所以前端无法使用 ${param.属性名}的方式
		    request.getRequestDispatcher("/goods/goods_edit.jsp").forward(request,response);
	    }
	} 
	
	

	//商品维护(查询商品列表)
	private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int bigCateId=request.getParameter("bigCateId")==null?0:Integer.parseInt(request.getParameter("bigCateId"));
		int smallCateId=request.getParameter("smallCateId")==null?0:Integer.parseInt(request.getParameter("smallCateId"));
		int pageIndex=request.getParameter("pageIndex")==null?1:Integer.parseInt(request.getParameter("pageIndex"));
		String goodsName=request.getParameter("goodsName"); 
		
		int rowCount=goodsDao.getGoodsCount(bigCateId,smallCateId,goodsName);
		
		PageInfo pageInfo=PageUtil.getPageInfo(rowCount, 15, pageIndex);
		List<GoodsInfo> goodsList= goodsDao.getGoodsList(bigCateId,smallCateId,goodsName,pageInfo);
		request.setAttribute("goodsList", goodsList);
		request.setAttribute("pageInfo", pageInfo);
		request.getRequestDispatcher("/goods/goods_manage.jsp").forward(request, response);	
	}

	
}
